package yawning.mew.gui.characters.talents;

import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextPane;
import javax.swing.SpinnerNumberModel;

import org.jdesktop.beansbinding.AutoBinding;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.Bindings;

import yawning.mew.gui.tools.CharacterDataSource;
import yawning.mew.gui.tools.TalentWrapper;

@SuppressWarnings("serial")
public class TalentContainer extends JPanel {
	TalentWrapper talent;
	private String talentName;
	SpinnerNumberModel model;
	JSpinner spnRank;
	JTextPane txtTalentName;
	/**
	 * Create the panel.
	 */
	public TalentContainer(String talentName) {
		setLayout(null);
		model = new SpinnerNumberModel(0, 0,0, 1);
		
		spnRank = new JSpinner();
		spnRank.setEnabled(false);
		spnRank.setModel(model);
		spnRank.setBounds(107, 0, 29, 20);
		add(spnRank);
		
		txtTalentName = new JTextPane();
		txtTalentName.setEditable(false);
		txtTalentName.setBounds(0, 0, 103, 20);
		add(txtTalentName);
		
		initDataBindings();
		setTalentName(talentName);
	}

	public void setTalent(CharacterDataSource source)
	{
		talent = source.getTalentByName(talentName);
		setMaxRank(talent.getMaxRank());
		spnRank.setValue(talent.getRank());
		setDescription(talent.getDescription());
	}
	/**
	 * @param maxRank the maxRank to set
	 */
	private void setMaxRank(int maxRank) {
		model.setMaximum(maxRank);
	}
	/**
	 * @return the maxRank
	 */
	public int getMaxRank() {
		return (Integer) model.getMaximum();
	}
	/**
	 * @param rank the rank to set
	 */
	public void setRank(int rank) {
		spnRank.setValue(rank);
		//TODO set new rank;
		setDescription(talent.getDescription());
	}
	/**
	 * @return the rank
	 */
	public int getRank() {
		return (Integer) spnRank.getValue();
	}
	/**
	 * @param description the description to set
	 */
	private void setDescription(String description) {
		this.setToolTipText(description);
		spnRank.setToolTipText(description);
		txtTalentName.setToolTipText(description);
	}
	/**
	 * @return the description
	 */
	public String getDescription() {
		return getToolTipText();
	}
	/**
	 * @param talentName the talentName to set
	 */
	private void setTalentName(String talentName) {
		this.talentName = talentName;
		txtTalentName.setText(talentName);
	}
	/**
	 * @return the talentName
	 */
	public String getTalentName() {
		return talentName;
	}
	
	protected void initDataBindings() {
		BeanProperty<TalentWrapper, Integer> talentWrapperBeanProperty = BeanProperty.create("rank");
		BeanProperty<JSpinner, Object> jSpinnerBeanProperty = BeanProperty.create("value");
		AutoBinding<TalentWrapper, Integer, JSpinner, Object> autoBinding = Bindings.createAutoBinding(UpdateStrategy.READ, talent, talentWrapperBeanProperty, spnRank, jSpinnerBeanProperty, "talentRank");
		autoBinding.bind();
		//
		BeanProperty<TalentWrapper, String> talentWrapperBeanProperty_1 = BeanProperty.create("description");
		BeanProperty<JPanel, String> jPanelBeanProperty = BeanProperty.create("toolTipText");
		AutoBinding<TalentWrapper, String, JPanel, String> autoBinding_1 = Bindings.createAutoBinding(UpdateStrategy.READ, talent, talentWrapperBeanProperty_1, this, jPanelBeanProperty, "TalentDescription");
		autoBinding_1.bind();
	}
}
